package ru.nosport.matrixaria.modules.user.rexample.client;

import ru.nosport.matrixaria.modules.common.AppServerProcess;
import ru.nosport.matrixaria.modules.common.Application;
import ru.nosport.matrixaria.modules.user.rexample.server.RemoteServer;
import ru.nosport.matrixaria.modules.user.rexample.server.DataContainer;
import ru.nosport.matrixaria.core.MessageHandler;
import ru.nosport.matrixaria.core.MessageHandlerException;
import ru.nosport.matrixaria.core.MLogger;
import org.apache.commons.configuration.Configuration;

import java.util.logging.Level;

/**
 * User: Victor Fabrichenko
 * Date: 24.12.2008
 * Time: 14:26:39
 */
public class ClientApp implements Application, Client {

	private RemoteServer rServer;
	private String rServerName;
	private DataContainer rServerResponseData;
	private DataContainer[] rServerResponseDataList;

	public ClientApp(Configuration _conf){
		this.rServerName = _conf.getString("remoteServerStubInstanceName");
		this.rServerResponseData = null;
		this.rServerResponseDataList = new DataContainer[0];
	}

	/**
	 *
	 * @return -
	 * @throws Exception
	 */
	public boolean moduleStart() throws Exception {
		try {
			this.rServer = (RemoteServer) MessageHandler.getModule(this.rServerName);

			return true;
		} catch (MessageHandlerException _ex) {
			_ex.printStackTrace();
			return false;
		}
	}

	/**
	 *
	 * @return -
	 */
	public DataContainer sendServerRequest(String _param){
		this.rServerResponseData = this.rServer.getDataContainer(_param);

		if(this.rServerResponseData == null){
			MLogger.get(this).log(Level.SEVERE, this.rServerName+".getDataContainer(\""+_param+"\") return null!");
			this.rServerResponseData = new DataContainer();
		}

		return this.rServerResponseData;
	}

	public DataContainer[] sendServerBigRequest(Integer _count){
		this.rServerResponseDataList = this.rServer.getDataContainerList(_count);

		return this.rServerResponseDataList;
	}

	/**
	 *
	 * @return -
	 */
	public boolean moduleStop() {
		try {
			MessageHandler.releaseModule(this.rServerName, this.rServer);
		} catch (MessageHandlerException e) {
			e.printStackTrace();
		}
		return true;
	}

	/**
	 *
	 * @return -
	 */
	public boolean isModuleRunning() {
		return true;
	}

	/**
	 *
	 * @return -
	 */
	public String moduleStat() {
		return "ClientApp stat.";
	}

	public boolean moduleSuspend() {
		return true;
	}

	public boolean moduleResume() {
		return true;
	}
}
